/*
 * MapFDao.java
 *
 * Created on 7 de abril de 2006, 16:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import fmap.DatosCompartidos;
import fmap.model.MapF;
import fmap.utils.IConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author javi
 */
public class MapFDao
{
    
    public MapFDao ()
    {
    }
    
    public boolean tieneHijos ( int idMapF)
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT COUNT(idNode) " +
                    "FROM fmap.node " +
                    "WHERE idMapF = ?");
            
            ps.setInt ( 1, idMapF );
            
            
            ResultSet rs = ps.executeQuery ();
            
            if ( rs.next () )
            {
                if ( rs.getInt (1) > 0 )    return true;
                else                        return false;
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () + "ERROR: MapFDao : tieneHijos" );
        }
        
        return true;
    }
    
    
    public MapF[] selectMapFCollection ( int idCollection)
    {
        
        MapF [] aMapF = null;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idMapF, idCollection, name, descrip, icon, nNod, path, idTable, idCat " +
                    "FROM fmap.mapF " +
                    "WHERE idCollection=?");
            
            ps.setInt (1,idCollection);
            
            
            ResultSet rs = ps.executeQuery ();
            
            Vector vMapF = new Vector ();
            while ( rs.next () )
            {
                
                vMapF.add (
                        new MapF (
                        rs.getInt ("idMapF"),
                        rs.getInt ("idCollection"),
                        rs.getString ("name"),
                        rs.getString ("path"),
                        rs.getString ("descrip"),
                        rs.getString ("icon"),
                        rs.getInt ("nNod"),
                        rs.getInt ("idTable"),
                        rs.getInt ("idCat")
                        ));
                
            }
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
            
            aMapF = new MapF[ vMapF.size () ];
            
            for( int i=0; i<vMapF.size (); i++)
            {
                aMapF[i] = (MapF)vMapF.get (i);
            }
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () + "ERROR: MapFDao : selectMapFCollection" );
        }
        
        return aMapF;
    }
    
    
    /**
     * Busca si el nombre y la descripcion de un mapa de ficheros coinciden con una cadena pasada => devolvemos true
     * Si no coincide devolvemos false
     * No tiene en cuenta mayusculas y minusculas
     */
    public boolean searchMapFName ( int idMapF, String name )
    {
        boolean returnValue = false;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idMapF " +
                    "FROM fmap.mapF " +
                    "WHERE idMapF=? AND ( UPPER(name) LIKE '%"+name.toUpperCase ()+"%' OR UPPER(descrip) LIKE '%"+name.toUpperCase ()+"%' )");
            
            ps.setInt ( 1, idMapF );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                returnValue = true;
            }
            
            rs.close ();
            ps.close ();
            
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return returnValue;
    }
    
    
    
    /**
     * Busca si el nombre y la descripcion de un mapa de ficheros coinciden con una cadena pasada => devolvemos true
     * Si no coincide devolvemos false
     * Tiene en cuenta mayusculas y minusculas
     */
    public boolean searchMapFNameCase ( int idMapF, String name )
    {
        boolean returnValue = false;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();

            PreparedStatement ps = conn.prepareStatement ( "" +
                    "SELECT idMapF " +
                    "FROM fmap.mapF " +
                    "WHERE idMapF=? AND ( name LIKE '%"+name+"%' OR descrip LIKE '%"+name+"%') ");
            
            ps.setInt ( 1, idMapF );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                returnValue = true;
            }
            
            rs.close ();
            ps.close ();
            
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () + "ERROR: NodeDao : searchNodesName" );
        }
        
        return returnValue;
    }
    
    
    public int selectMaxIdMapF ()
    {
        int idMaxMapF = -1;
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            PreparedStatement ps = conn.prepareStatement ( "SELECT MAX(idMapF) FROM fmap.mapF" );
            
            ResultSet rs = ps.executeQuery ();
            
            if( rs.next () )
            {
                idMaxMapF = rs.getInt (1);
            }
            
            
            rs.close ();
            ps.close ();
//            conn.close ();
            
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
//            System.out.println ( e.getMessage () + "ERROR: MapFDao : selectMapFCollection" );
        }
        
        return idMaxMapF;
    }
    
    
    public MapF insertMapF ( MapF mapF )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "INSERT INTO fmap.mapF " +
                    "(idCollection, name, descrip, icon, nNod, path, idTable, idCat) " +
                    "VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
            System.out.println ( "ICONO CUANDO GUARDAMOS EL MAPA DE FICHEROS"+mapF.getIcon () );
            ps.setInt (1, mapF.getIdCollection ());
            ps.setString (2, mapF.getName ());
            ps.setString (3, mapF.getDesc ());
            ps.setString (4, mapF.getIcon ());
            ps.setInt (5, mapF.getNumNod ());
            ps.setString (6, mapF.getPath ());
            ps.setInt (7, mapF.getIdTable ());
            ps.setInt (8, mapF.getIdCat ());
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
            
            mapF.setIdMapF ( this.selectMaxIdMapF () );
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }
        
        return mapF;
    }
    
    
    
    public MapF updateMapF ( MapF mapF )
    {
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            PreparedStatement ps = conn.prepareStatement ( "" +
                    "UPDATE fmap.mapF SET idCollection = ?, name = ?, descrip = ?, icon = ?, nNod = ?, path = ?, idCat = ? WHERE idMapF=?");
            
            ps.setInt ( 1, mapF.getIdCollection () );
            ps.setString ( 2, mapF.getName () );
            ps.setString ( 3, mapF.getDesc () );
            ps.setString ( 4, mapF.getIcon () );
            ps.setInt ( 5, mapF.getNumNod () );
            ps.setString ( 6, mapF.getPath () );
            ps.setInt (7, mapF.getIdCat ());
            ps.setInt ( 8, mapF.getIdMapF () );
//            ps.setInt ( 8, mapF.getIdTable () );
            
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return null;
//            System.out.println ( e.getMessage () );
        }
        
        return mapF;
    }
    
    
    
    public boolean deleteMapFCollection ( int idCollection )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            // Eliminamos todos los mapas de ficheros de la coleccion
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.mapF WHERE idCollection=?");
            ps.setInt ( 1, idCollection );
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
    
    public boolean deleteMapF ( int idMapF )
    {
        
        try
        {
            Connection conn = ConnectionDao.getConnection ();
            
            // Eliminamos todos los mapas de ficheros de la coleccion
            PreparedStatement ps = conn.prepareStatement ( "DELETE FROM fmap.mapF WHERE idMapF=?");
            ps.setInt ( 1, idMapF );
            ps.executeUpdate ();
            
            ps.close ();
//            conn.close ();
        }
        catch( Exception e)
        {
            DatosCompartidos.getJFrameMain ().setLblMsj ( IConstants.ERROR );
            return false;
//            System.out.println ( e.getMessage () );
        }
        
        return true;
    }
    
}
